package com.yuch.config.security;

import com.yuch.dao.UserDao;
import com.yuch.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @projectName: springboot_31_ActiveMQ
 * @author: yuch
 * @description: TODO
 * @date: 2024/8/5 11:03
 */


@Repository
public class MyuserDetail implements UserDetailsService {

    @Autowired
    private UserDao userDao;

    /**
     * 根据用户名去查询这个这个用户的信息
     * 我们需要自己去实现这个方法，从数据库里面查询用户信息
     * 并且把用户信息封装成UserDetails对象返回
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */

    @Override

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        System.out.println("username:" + username);
        User byUserName = userDao.findByUserName(username);
        if (Objects.isNull(byUserName)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //用户存在封装成 userdetails对象返回
        UserDetails loginUser = new LoginUser(byUserName);
        return loginUser;


    }
}
